package gs.es.plugin.safe;

import org.elasticsearch.common.Booleans;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.rest.RestModule;

/**
 * Created by eviltuzki on 17-1-19.
 */
public class SafePlugin extends Plugin {
    private final Settings settings;

    public SafePlugin(Settings settings) {
        this.settings = settings;
    }

    @Override
    public String name() {
        return "Safe";
    }

    @Override
    public String description() {
        return "ElasticSearch Safe Plugin";
    }

    public void onModule(RestModule module) {
        boolean open = settings.getAsBoolean("es.plugin.safe.open",true);//插件是否开启，默认开启
        boolean delete = settings.getAsBoolean("es.plugin.safe.onlyWhiteListAllowDelete",false);//只允许白名单删除，所有都可以访问
        boolean access = settings.getAsBoolean("es.plugin.safe.onlyWhiteListAllowAccess",true);//只有白名单可以访问，其他都拒绝访问
        String[] whiteList = settings.getAsArray("es.plugin.safe.whiteList");//IP白名单
        int from = settings.getAsInt("es.plugin.safe.from",10000);
        int size = settings.getAsInt("es.plugin.safe.size",10000);
        System.out.println("Load Es Safe Plugin");
        DataStore.setDelete(delete);
        DataStore.setAccess(access);
        DataStore.setFrom(from);//设置From最大值
        DataStore.setSize(size);//设置Size最大值
        DataStore.setIsOpen(open);//插件是否开启
        for (String ip : whiteList) {//加载白名单列表
            DataStore.getWhiteList().add(ip);
        }
        module.addRestAction(SafePluginHandler.class);
    }
}
